<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="Field Name" disabled :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.fieldName" placeholder="请输入Field Name"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="Data type" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag v-model="model.type" placeholder="请选择数据类型" dict-code="data_type" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>
  
  <script>
import vueJsonEditor from 'vue-json-editor'
import store from '@/store/'
import dynamicForm from './dynamicForm'
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
const mappingRe = {
  text: 'text',
  float: 'text',
  date: 'datePicker',
  long: 'text',
  boolean: 'select',
  integer: 'text',
  object: 'inputJOSN',
  keyword: 'text',
}
const es6mappings = {
  'Text: Aggs': {
    type: 'text',
    fields: {
      keyword: {
        type: 'keyword',
        index: 'true',
        ignore_above: 256,
      },
      english: {
        type: 'text',
        analyzer: 'english',
      },
    },
    analyzer: 'standard',
    search_analyzer: 'standard',
  },
  'Text: Search': {
    type: 'text',
    fields: {
      keyword: {
        type: 'keyword',
        index: 'true',
        ignore_above: 256,
      },
      search: {
        type: 'text',
        index: 'true',
        analyzer: 'ngram_analyzer',
        search_analyzer: 'simple',
      },
      autosuggest: {
        type: 'text',
        index: 'true',
        analyzer: 'autosuggest_analyzer',
        search_analyzer: 'simple',
      },
      english: {
        type: 'text',
        analyzer: 'english',
      },
    },
    analyzer: 'standard',
    search_analyzer: 'standard',
  },
  Long: {
    type: 'long',
  },
  Integer: {
    type: 'integer',
  },
  Double: {
    type: 'double',
  },
  Float: {
    type: 'float',
  },
  Date: {
    type: 'date',
    format: 'YYYY-MM-DD',
  },
  Boolean: {
    type: 'boolean',
  },
  'Geo Point': {
    type: 'geo_point',
  },
  'Geo Shape': {
    type: 'geo_shape',
  },
  object: {
    type: 'object',
  },
  Image: {
    type: 'text',
    fields: {
      keyword: {
        type: 'keyword',
        index: 'true',
      },
    },
  },
  Keyword: {
    type: 'keyword',
    ignore_above: 256,
  },
}
export default {
  name: 'ChatVectorFileCabinetForm',
  components: {
    vueJsonEditor,
    dynamicForm,
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      activeId: '1',
      docIndex: '',
      configId: '',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
      fieldOptions: [],
      confirmLoading: false,
      validatorRules: {
        cabinetName: [{ required: true, message: '请输入文件柜名称' }],
      },

      url: {
        add: '/es/achievement_index/_mapping',
        edit: '/es/achievement_index/_mapping',
        queryById: '/ChatVectorFileCabinet/chatVectorFileCabinet/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(index, mappings) {
      this.configId = index
      this.edit()
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    returnType(type) {
      return mappingRe[type]
    },
    handleInputJosnChange(e) {
      console.log(e)
    },
    submitForm() {
      console.log(this.model)
      let obj = {
        properties: {
          [this.model.fieldName]: es6mappings[this.model.type],
        },
      }
      const that = this

      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = `/es/${this.configId}/_mapping`
          let method = ''
          if (!this.model.id) {
            method = 'post'
          } else {
            method = 'post'
          }
          httpAction(httpurl, obj, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
